﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YunSpace.Core;

namespace YunSpace.Web.Controllers
{
    public class LogsController : Controller
    {
        // GET: Logs
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFileJson()
        {
            var basePath = System.AppDomain.CurrentDomain.BaseDirectory;
            var files = FileHelper.GetFileNames(Path.Combine(basePath, "Logs"), "*", true);
            var allFiles = new List<Tuple<string, string>>();
            foreach (var item in files)
            {
                var fileName = Path.GetFileName(item);
                var fileFolder = item.Replace(basePath, "");
                var filePath = Path.Combine(Url.Content("~/"), fileFolder);
                allFiles.Add(Tuple.Create<string, string>(fileFolder, filePath));
;            }

            var data = new
            {
                files = allFiles
            };
            return Content(data.ToJson());
        }
    }
}